<?php
$id = $_POST['id'];
$cdk = $_POST['cdk'];
require('../../cofd/common.php');
$stmt = $conn->prepare("SELECT * FROM exam_cdk WHERE cdk = ?");
if ($stmt === false) {
    echo json_encode(array('status' => 500, 'msg' => '准备查询失败: ' . $mysqli->error));
    exit();
}
$stmt->bind_param('s', $cdk); 
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $rows = $row;
    }
} else {
    echo json_encode(array('status' => 500, 'msg' => '未找到该CDK'));
    exit();
}
if($rows['sycs'] >= $rows['xzcs']){
    echo json_encode(array('status' => 500, 'msg' => '该CDK已被使用'));
    exit();
}
$stmt = $conn->prepare("UPDATE exam_cdk SET sycs=sycs+1 WHERE cdk=?");
if ($stmt === false) {
    echo json_encode(array('status' => 500, 'msg' => '更新失败: ' . $mysqli->error));
    exit();
}
$stmt->bind_param('s', $cdk); 
if($stmt->execute()){
    $gettime = $rows['getime'];
    $tell = $rows['tell'];
    $stmt = $conn->prepare("SELECT vip FROM exam_user WHERE id=?");
    if ($stmt === false) {
        echo json_encode(array('status' => 500, 'msg' => '准备查询失败: ' . $mysqli->error));
        exit();
    }
    $stmt->bind_param('s', $id); 
    $stmt->execute();
    $result = $stmt->get_result();
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $vip = $row;
        }
    } else {
        echo json_encode(array('status' => 500, 'msg' => '未找到该用户'));
        exit();
    }
    if(strtotime($vip['vip']) < time()){
        $newtime = strtotime(date('Y-m-d H:i:s',time())) + $gettime*60;
    }else{
        $newtime = strtotime($vip['vip']) + $gettime*60;
    }
    $newtime = date('Y-m-d H:i:s',$newtime);
    $stmt = $conn->prepare("UPDATE exam_user SET vip=? WHERE id=?");
    if ($stmt === false) {
        echo json_encode(array('status' => 500, 'msg' => '更新失败: ' . $mysqli->error));
        exit();
    }
    $stmt->bind_param('ss', $newtime,$id); 
    if($stmt->execute()){
        echo json_encode(array('status' => 200, 'msg' => $tell));
    }
}else{
    echo json_encode(array('status' => 500, 'msg' => '更新失败: ' . $mysqli->error));
}
$stmt->close();
$conn->close();
?>